﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DreamOnGameEngine.Main
{
    /// <summary>
    /// Type of the process
    /// </summary>
   public enum PROCESS_TYPE
    {
        PROC_NONE,
        PROC_WAIT,
        PROC_CONTROL,
        PROC_SCREEN,
        PROC_MUSIC,
        PROC_SOUNDFX,
        PROC_ACTOR,
        PROC_INTERPOLATOR,
        //// These AI state proc id's shouldn't live here!  Game-specific stuff in the engine is bad!  I promise I'll move them....
        //PROC_AISTATE_ATTACK,
        //PROC_AISTATE_CHASETARGET,
        //PROC_AISTATE_WANDER,
        //PROC_AISTATE_SPIN,
        //PROC_AISTATE_WAIT,
        //PROC_GAMESPECIFIC,

        PROC_REALTIME,		// added for Chapter 18!
    };
    /// <summary>
    /// A single process
    /// </summary>
    public class CProcess
    {
        const int PROCESS_FLAG_ATTACHED=1;
        protected PROCESS_TYPE m_iType;
        protected bool m_bKill;
        protected bool m_bActive;
        protected bool m_bPaused;
        protected bool m_bInitialUpdate;	// initial update?
        protected CProcess m_pNext;
        private uint m_uProccessFlags;
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="nType">Type of this process</param>
        /// <param name="uOrder">not quite sure</param>
        public CProcess(PROCESS_TYPE nType, uint uOrder = 0)
        {
            m_iType = nType;
            m_bKill = false;
            m_bActive = true;
            m_uProccessFlags = 0;
            m_bPaused = false;
            m_bInitialUpdate = true;
        }
        /// <summary>
        /// Deconstructs and stuff
        /// </summary>
         ~CProcess()
        {
        }
        /// <summary>
        /// Is this dead?
        /// </summary>
        public bool Dead
        {
            get { return m_bKill; }
        }
        /// <summary>
        /// Kills the process
        /// </summary>
        public virtual void VKill()
        {
            m_bKill=true;
        }
        /// <summary>
        /// Type of Process
        /// </summary>
        public PROCESS_TYPE PType
        {
            get { return m_iType; }
            set { m_iType = value; }
        }
        /// <summary>
        /// Is the process currently active?
        /// </summary>
        public bool Active
        {
            get { return m_bActive; }
            set { m_bActive = value; }
        }
        /// <summary>
        /// Attached to the manager?
        /// </summary>
        public bool Attached
        {
            get { return ((m_uProccessFlags & PROCESS_FLAG_ATTACHED)!=0) ? true : false; }
            set
            {
                if (value)
                    m_uProccessFlags |= PROCESS_FLAG_ATTACHED;
                else
                    m_uProccessFlags &= PROCESS_FLAG_ATTACHED;
            }
        }
        /// <summary>
        /// Is the process paused?
        /// </summary>
        public bool Paused
        {
            get { return m_bPaused; }
        }
        /// <summary>
        /// Toggles Process Pause Status
        /// </summary>
        public virtual void TogglePause()
        {
            m_bPaused = !m_bPaused;
        }
        /// <summary>
        /// Has the process run yet?
        /// </summary>
        public bool Initialized
        {
            get { return !m_bInitialUpdate; }
        }
        /// <summary>
        /// Process to run once this is dead
        /// </summary>
        public CProcess Next
        {
            get
            {
                return m_pNext;
            }
            set
            {
                m_pNext = value;
            }
        }
        /// <summary>
        /// Updates the process
        /// </summary>
        /// <param name="deltaMilliseconds">Milliseconds since last update</param>
        public virtual void VOnUpdate(int deltaMilliseconds)
        {
            if (m_bInitialUpdate)
            {
                VOnInitialize();
                m_bInitialUpdate = false;
            }
        }
        /// <summary>
        /// Initiialize the process
        /// </summary>
        public virtual void VOnInitialize()
        {
        }

    }
}
